# Filters added to this controller apply to all controllers in the application.
# Likewise, all the methods added will be available for all controllers.

class ApplicationController < ActionController::Base

  before_filter :check_iphone

  helper :all # include all helpers, all the time

  # See ActionController::RequestForgeryProtection for details
  # Uncomment the :secret if you're not using the cookie session store
  protect_from_forgery # :secret => '3783c69eaac9ee1777eb0a1bf68ef882'

  # Be sure to include AuthenticationSystem in Application Controller instead
  include AuthenticatedSystem

  def user_admin_required
    if !(logged_in? && current_user.has_role?("Administrator", klass))
      flash[:alert] = "You must be logged in as an Administrator to perform this operation."
      redirect_to new_session_path
    end
  end

protected

  def iphone_login_required
    if iphone?
      redirect_to new_session_path unless logged_in?
    end
  end

  def iphone?
    return request.env["HTTP_USER_AGENT"] && request.env["HTTP_USER_AGENT"][/(Mobile\/.+Safari)/]
  end  

  def check_iphone
    if iphone? || params[:format] == "iphone"
      request.format = :iphone 
    end  
  end

end
